package com.hibase.upms.controller;


import java.util.List;

import com.hibase.upms.entity.user.dto.*;
import com.hibase.upms.entity.user.vo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.common.util.UserContext;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.upms.constant.UserManagerConstant;
import com.hibase.upms.entity.user.MdsUser;
import com.hibase.upms.entity.user.vo.MdsUserVO;
import com.hibase.upms.service.user.MdsUserService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ChrisCui
 * @since 2019-04-16
 */
@RestController
@RequestMapping("/mds-user")
@Api(tags = "用户管理")
@Slf4j
@RequiredArgsConstructor
public class MdsUserController {

    private final MdsUserService userService;

    @ApiOperation(value = "新建用户", httpMethod = "POST", response = MdsUser.class)
    @HibasePostRequestMapping(value = "/addUserInfo")
//    @PreAuthorize("@pms.hasPermission('user_add')")
    public ResponseModel addUserInfo(@RequestBody MdsUserInsertVO userInsertVO) {
        MdsUser mdsUser = userService.changeUser(EntityUtil.transform(userInsertVO, MdsUserUpdateVO.class));
        return ResponseModel.successMsg(mdsUser.getId());
    }

    @ApiOperation(value = "更新用户", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updateUserInfo")
    public ResponseModel updateUserInfo(@RequestBody MdsUserUpdateVO userUpdateVO) {
        MdsUser mdsUser = userService.changeUser(userUpdateVO);
        return ResponseModel.successMsg(mdsUser.getId());
    }

    @ApiOperation(value = "启用用户", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/enableUserInfo")
    public ResponseModel enableUser(@RequestBody OperationIdsEntity userIdsVo) {
        userService.changeUserStatus(userIdsVo.getIds(), UserManagerConstant.RECORD_STATUS_ENABLE);
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "停用用户", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/disabledUserInfo")
    public ResponseModel disabledUser(@RequestBody OperationIdsEntity userIdsVo) {
        userService.changeUserStatus(userIdsVo.getIds(), UserManagerConstant.RECORD_STATUS_DISABLED);
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询用户列表", httpMethod = "POST", response = MdsUserVO.class)
    @HibasePostRequestMapping(value = "selectList")
    public ResponseModel selectList(@RequestBody MdsUserSearchVO searchVO) {
        PageInfo result = userService.selectList(searchVO);
        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "查询用户详情", httpMethod = "POST", response = MdsUserViewVO.class)
    @HibasePostRequestMapping(value = "viewDetails")
    public ResponseModel viewDetails(@RequestBody OperationIdEntity idEntity) {
        return ResponseModel.successMsg(userService.viewDetails(idEntity.getId()));
    }

    @ApiOperation(value = "查询用户绑定角色列表", httpMethod = "POST", response = MdsUserRoleViewVO.class)
    @HibasePostRequestMapping(value = "viewRoleDetails")
    public ResponseModel viewRoleDetails(@RequestBody OperationIdEntity idEntity) {
        PageInfo result = userService.selectUserRoleList(idEntity);
        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "修改用户密码", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updatePasswd")
    public ResponseModel updatePasswd(@RequestBody MdsUserUpdatePwdVO userUpdatePwdVO) {
        userService.updatePasswd(userUpdatePwdVO.getOldPasswd(), userUpdatePwdVO.getNewPasswd());
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "重置密码", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/resetPasswd")
    public ResponseModel<String> resetPasswd(@RequestBody MdsUserResetPwdVO userResetPwdVO) {
        userService.resetPasswd(userResetPwdVO.getId(), userResetPwdVO.getPasswd());
        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "查询作业人员用户列表", httpMethod = "POST", response = MdsUserVO.class)
    @HibasePostRequestMapping(value = "selectOperatorUserList")
    public ResponseModel selectOperatorUserList(@RequestBody MdsUserSearchVO searchVO) {
        PageInfo result = userService.selectList(searchVO);
        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "查询当前下级组织所有用户列表", httpMethod = "POST", response = MdsUserVO.class)
    @HibasePostRequestMapping(value = "selectChildUserList")
    public ResponseModel selectChildUserList(@RequestBody MdsUserSearchVO searchVO) {
        PageInfo result = userService.selectChildUserList(searchVO);
        result.setList(EntityUtil.transform(result.getList(), MdsUserVO.class));
        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新建用户", httpMethod = "POST", response = MdsUser.class)
    @HibasePostRequestMapping(value = "/insertUser")
//    @PreAuthorize("@pms.hasPermission('user_add')")
    public ResponseModel insertUser(@RequestBody MdsUserUpdateMultiVO userInsertVO) {
        MdsUser mdsUser = userService.changeUser(userInsertVO);
        //此处需要返回对象信息
        return ResponseModel.successMsg(mdsUser.getId());
    }

    @ApiOperation(value = "更新用户", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updateUser")
    public ResponseModel updateUser(@RequestBody MdsUserUpdateMultiVO userUpdateVO) {
        MdsUser mdsUser = userService.changeUser(userUpdateVO);
        //此处需要返回对象信息
        return ResponseModel.successMsg(mdsUser.getId());
    }

    @ApiOperation(value = "当前用户所拥有的资源", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/selectNameByUserName")
    public ResponseModel selectNameByUserName(@RequestBody MdsUserSearchVO searchVo) {
        HibaseAssert.isFalse(StrUtil.isBlank(searchVo.getLoginName()), "登录名不能为空");
        List<String> permissionList = userService.selectNameByUserName(searchVo.getLoginName());
        //此处需要返回对象信息
        return ResponseModel.successMsg(permissionList);
    }

    /**
     * @param updateVo
     * @return
     * @Title: MdsUserController
     * @Description: 修改用户头像
     * @author TimBrian
     * @since 2021年9月6日 上午10:36:38
     */
    @ApiOperation(value = "修改用户头像", httpMethod = "POST", response = ResponseModel.class)
    @HibasePostRequestMapping(value = "/updateUserHeadImg")
    public ResponseModel updateUserHeadImg(@RequestBody MdsUserUpdateHeadImgVO updateVo) {
        HibaseAssert.isFalse(StrUtil.isBlank(updateVo.getHeadThumb()), "用户头像不能为空");
        MdsUser mdsUser = new MdsUser();
        mdsUser.setId(UserContext.get().getId());
        mdsUser.setHeadThumb(updateVo.getHeadThumb());
        boolean update = userService.updateById(mdsUser);
        //此处需要返回对象信息
        return ResponseModel.successMsg(update);
    }
}